package net.yeah.mickey_zhouyou.module.booklist.domain;

import java.util.Collection;

import net.yeah.mickey_zhouyou.database.DBUI;
import net.yeah.mickey_zhouyou.database.domain.BaseEntity;

public class Category extends BaseEntity {

	private String text;
	private Long parentId;

	@Override
	public String toString() {
		return text;
	}

	public Category() {
	}

	public Category(String text) {
		this.text = text;
	}

	public Collection<Category> children() {
		return DBUI.load(Category.class, "parentId", super.getId());
	}

	public Category getParent() {
		return parentId == null ? null : DBUI.load(Category.class, parentId);
	}

	public void setParent(Category category) {
		if (category == null)
			parentId = null;
		else
			parentId = category.getId();
	}

	public String getText() {
		return text;
	}

	public void setText(String text) {
		this.text = text;
	}

	@Override
	public int compare(BaseEntity o1, BaseEntity o2) {
		if (o1 instanceof Category && o2 instanceof Category) {
			Category c1 = Category.class.cast(o1);
			Category c2 = Category.class.cast(o2);
			return c1.getText().compareTo(c2.getText());
		}
		throw new UnsupportedOperationException();
	}

}
